//---------------------------------------------------------------------
// Author: Keith Hill, jachymko
//
// Description: Class to implement the Convet-Xml cmdlet which applies
//              the supplied XSL tranform to the supplied XML.
//
// Creation Date: Sept 24, 2006
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Management.Automation;
using System.Net;
using System.Security;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using Pscx.Commands.Internal;

namespace Pscx.Commands.Xml
{
    [Cmdlet(VerbsData.Convert, PscxNouns.Xml, DefaultParameterSetName = ParameterSetPath)]
    [Description("Performs XSLT transforms on the specified XML file or XmlDocument.")]
    [DetailedDescription("Performs XSLT transforms on the specified XML file or XmlDocument.  Use the EnableScript parameter to enable script embedded in the XSLT file.")]
    [RelatedLinks("Test-Xml")]
    [RelatedLinks("Format-Xml")]
    public class ConvertXmlCommand : XmlCommandBase
    {
        private XslCompiledTransform _xslTransform;
        private SwitchParameter _enableScript;
        private string _xsltPath;

        [ValidateNotNullOrEmpty,
         Parameter(Position = 1, Mandatory = true, HelpMessage = "Path to the XSLT file to apply during the transform."),
         AcceptsWildcards(false)]
        public string XsltPath
        {
            get { return _xsltPath; }
            set { _xsltPath = value; }
        }

        [Parameter(HelpMessage = "Enable embedded script blocks in the XSLT.")]
        public SwitchParameter EnableScript
        {
            get { return _enableScript; }
            set { _enableScript = value; }
        }

        protected virtual XsltSettings XsltSettings
        {
            get { return new XsltSettings(false, _enableScript); }
        }

        protected virtual XmlUrlResolver XmlUrlResolver
        {
            get
            {
                XmlUrlResolver resolver = new XmlUrlResolver();
                resolver.Credentials = CredentialCache.DefaultCredentials;

                return resolver;
            }
        }

        protected override void BeginProcessing()
        {
            base.BeginProcessing();

            try
            {
                string xslt = GetUnresolvedProviderPathFromPSPath(_xsltPath);

                _xslTransform = new XslCompiledTransform();
                _xslTransform.Load(xslt, XsltSettings, XmlUrlResolver);
            }
            catch (XsltException exc)
            {
                WriteError(PscxErrorRecord.XsltError(exc));
            }
            catch (Exception exc)
            {
                WriteError(PscxErrorRecord.FileError(exc, _xsltPath));
            }
        }

        protected override void ProcessXmlReader(XmlReader xmlReader, XmlWriter xmlWriter)
        {
            try
            {
                _xslTransform.Transform(xmlReader, xmlWriter);
            }
            catch (Exception exc)
            {
                WriteError(PscxErrorRecord.XsltError(exc));
                return;
            }
        }
    }
}